<?php
/**
 *
 */
namespace app\common\query;
use app\common\model\OrderLogistics as OrderLogisticsModel;
use app\common\cache\CacheData;
class OrderLogistics
{
    static $status = [
                1 => '待发货',
                2 => '待签收',
                3 => '订单完成',
                0 => '订单取消',
            ];

    public static function status($key=null){
        if(!is_null($key)){
            return self::$status[$key];
        }else{
            return self::$status;
        }
    }
    /**
     * 获取物流货单
     * @param  [type] $where    [description]
     * @param  string $queryStr [description]
     * @param  string $baseUrl  [description]
     * @return [type]           [description]
     */
    public static function getRows($where=[],$queryStr='',$baseUrl=''){
        if(!$baseUrl){
            $baseUrl = url('orderlogistics.show/index');
        }
        // 每页数量
        $perpagenum = Option::get('perpage_goods_num')?Option::get('perpage_goods_num'):15;
        $rows = self::getDatas($where,'',$perpagenum);
        $total = $rows['total'];
        $page = $rows['current_page'];
        $rows['page_html'] = pagination($total, $perpagenum, $page, $baseUrl, $queryStr);
        return $rows;
    }


    /**
     * 查询多条数据
     * @return [type] [description]
     */
    public static function getDatas($where=[],$fields='',$perpagenum=null){
        $model = new OrderLogisticsModel;
        if($perpagenum){
            $data = $model->where($where)->order('create_time desc')->field($fields)->paginate($perpagenum);
            $data = $data->toArray();
            $rows = $data['data'];
            foreach($rows as &$row):
                $row = self::setRemark($row);
            endforeach;
            $data['data'] = $rows;
        }else{
            $data = $model->where($where)->order('create_time desc')->field($fields)->select();
            $data = $data->toArray();
            foreach($data as &$row):
                $row = self::setRemark($row);
            endforeach;
        }
        return $data;
    }

    /**
     * 查询一条物流货单
     */
    public static function getOneDatas($where=[],$field=''){
        $model = new OrderLogisticsModel;
        $data = $model->where($where)->field($field)->find();
        $row = self::setRemark($data);
        return $row;
    }

    /**
     * 处理字段数据
     * @param [type] $row [description]
     */
    public static function setRemark($row){
        if(isset($row['logis_id'])){
             $row['logisname']=\app\common\query\Logistics::getTitle($row['logis_id']);
        }
        return $row;
    }

    public static function del($data=[]){
        $model = new OrderLogisticsModel;
        return $model->delete($data);
    }
}